<?php

class members extends Controller
{
	public function orgs ($orgname = null, $feature = null, $id = null) {
		$this->session();
		if(isset($orgname)) {	
			$_SESSION['orgname'] = $orgname;
			$model = new member();
			$amIMember = $model->amIMember($_SESSION['stud_no'], $orgname);
			$model2 = new organization();
			$orgData =  $model2->getOrgByName($orgname);
			$orgData = count($orgData);
			if ($orgData > 0) {
				if($amIMember)	{
					if( isset($feature) ) {
					  if(isset($id)) $this->features($orgname, $feature , $id);
					  else $this->features($orgname, $feature);
					}
					else {
							$data['title'] = new BufferedView('orgPage/titles/torgSiteHome');	
							$data['content'] = new BufferedView('orgPage/orgSiteHome');
							new View ("orgPage/orgSiteTemplate", $data);
					}
				}
				else {
				  //view template for non-members
					$prompt['message'] = "YOu are a non-member!";
					new View ("emptyTemplate", $prompt);
				}
			}
			else {
				$prompt['message'] = "Such Organization doesn't exist.";
				new View ("members/template", $prompt);
			}
		}
		else {
			$this->orgindex();
		}
	}
	
	private function orgindex () {
		$data['title'] = 'Members: Organizations';	
		$model = new member();
		$orgid = $model->getMyOrgs($_SESSION['stud_no']);
		$orgno = count($orgid);
		if ($orgno > 0) {		
			$data['content'] = new BufferedView('members/membersOrgs',array(
			"organization" => $orgid
			) );
		}
		else {
			$data['content'] = new BufferedView('members/membersOrgs',
			"You dont have any Organizations" );
		}
		new View ("members/template", $data);
	}	

	private function features($orgname,$feature,$id = null) {
		if ($feature === "activities") {	
			if(isset($id)) {
				$data['title'] = new BufferedView('orgPage/titles/torgSiteJoinAct');
				$model = new event();
				$model2 = new commentevent();
				$model3 =  new registration();
				$orgEventbyId = $model->getEventbyId($id);
				$regList = $model3->getRegList();
				$comments = $model2->getComment($orgEventbyId[0]['event_id']);
				$data['content'] = new BufferedView('orgPage/orgSiteJoinAct',array(
					"activities" => $orgEventbyId,
					"registration" => $regList,
					"comments" => $comments
				) );
			}
			else {
				$data['title'] = new BufferedView('orgPage/titles/torgSiteActivities');
				$model = new event();
				$orgEvents = $model->getEvent($orgname);
				$data['content'] = new BufferedView('orgPage/orgSiteActivities',array(
					"activities" => $orgEvents
				) );
			}
			new View ("orgPage/orgSiteTemplate", $data);
		}
		else if ($feature === "announcements") {
			if(isset($id)) {
				$data['title'] = new BufferedView('orgPage/titles/torgSiteviewAncmt');
				$model = new article();
				$model2 = new commentarticle();
				$orgArticlebyId = $model->getArticlebyId($id);
				$comments = $model2->getComment($orgArticlebyId[0]['article_id']);
				$data['content'] = new BufferedView('orgPage/orgSiteviewAncmt',array(
					"announcement" => $orgArticlebyId,
					"comments" => $comments
				) );
			}
			else {
				$data['title'] = new BufferedView('orgPage/titles/torgSiteAnnouncements');
				$model = new article();
				$orgAnnouncements = $model->getArticle($orgname);
				$data['content'] = new BufferedView('orgPage/orgSiteAnnouncements',array(
					"announcements" => $orgAnnouncements
				) );
			}
			new View ("orgPage/orgSiteTemplate", $data);
		}
		else if ($feature === "tutorials" ) {
			if(isset($id)) {
				$data['title'] = new BufferedView('orgPage/titles/torgSiteVTutorials');
				$model = new tutorial();
				$model2 = new commenttutorial();
				$orgTutorialbyId = $model->getTutorialbyId($id);
				$comments = $model2->getComment($orgTutorialbyId[0]['topic_id']);
				$data['content'] = new BufferedView('orgPage/orgSiteVTutorial',array(
					"tutorial" => $orgTutorialbyId,
					"comments" => $comments
				) );
			}
			else {
				$data['title'] = new BufferedView('orgPage/titles/torgSiteTutorials');
				$model = new tutorial();
				$orgTutorials = $model->getTutorial($orgname);
				$data['content'] = new BufferedView('orgPage/orgSiteTutorials',array(
					"tutorials" => $orgTutorials
				) );
			}
			new View ("orgPage/orgSiteTemplate", $data);
		}
		else if ($feature === "forums") {
			$model = new forum();
			$orgForums = $model->getForums($orgname);

			if(isset($id)) {
				$data['title'] = new BufferedView('orgPage/titles/torgSiteThreads');
				$threads = $model->getThreads($id);
				$data['content'] = new BufferedView('orgPage/orgSiteThreads',array(
				"threads" => $threads
				) );


			}
			else {
				$data['title'] = new BufferedView('orgPage/titles/torgSiteForum');
				$data['content'] = new BufferedView('orgPage/orgSiteForum',array(
				"forums" => $orgForums
				) );
			}
			new View ("orgPage/orgSiteTemplate", $data);
		}
		else if ($feature === "threads" ) { 
			if(isset($id)) {
				$model = new forum();
				$data['title'] = new BufferedView('orgPage/titles/torgSitePost');
				$post = $model->getPosts($id);
				$data['content'] = new BufferedView('orgPage/orgSitePost',array(
				"posts" => $post
				) );
			}
			new View ("orgPage/orgSiteTemplate", $data);
		}
	}
}

